import { useReducer } from 'react';
/**
 * 定义初始状态信息
 * @loading 加载标记
 */
export interface IstateType {
  materialList: Record<string, any>[];
  dataSource: Record<string, any>[];
  BottomDataSource: Record<string, any>[];
}

/**
 * 更新需求信息操作枚举
 * @UPDATE_MATERIAL_LIST 更新需求物料数据
 */
export enum IActionEnum {
  UPDATE_MATERIAL_LIST = 'update_material_list',
  UPDATE_DATASOURCE = 'update_dataSource',
  SET_BottomDataSource = 'SET_BottomDataSource',
}

/**
 * 定义更新合同信息操作匹配
 * @type 操作类型
 * @payload 传递数据
 */
export interface IActionType {
  type: IActionEnum;
  payload?: any;
}

/** 定义需求新增和编辑状态管理 */
export const useNeedUpdateReducer = () => {
  /** 初始化状态 */
  const initState: IstateType = {
    materialList: [],
    dataSource: [],
    BottomDataSource: [],
  };

  /** 状态管理逻辑处理 */
  const updateReducer = (state: IstateType, action: IActionType) => {
    switch (action?.type) {
      case IActionEnum.UPDATE_MATERIAL_LIST:
        return {
          ...state,
          materialList: action?.payload,
        };
      case IActionEnum.UPDATE_DATASOURCE:
        return {
          ...state,
          dataSource: action?.payload,
        };
      case IActionEnum.SET_BottomDataSource:
        return {
          ...state,
          BottomDataSource: action?.payload,
        };
      default:
        return {
          ...state,
          ...initState,
        };
    }
  };

  const [state, dispatch] = useReducer(updateReducer, initState);
  return {
    ...state,
    dispatch,
  };
};
